home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Subject: Re: size of vtable
- From: ufo@sbbs.se (Urban Fosseus)
- X-Newsreader: WinVN 0.99.7
- References: <4cr1ob$1cu@seal.abalon.se>
- MIME-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- NNTP-Posting-Host: ppp22.sbbs.se
- Message-ID: <30f2dee6.0@news.sbbs.se>
- Date: 9 Jan 96 21:32:22 GMT
- Path: news.sbbs.se!
-
- In article <4cr1ob$1cu@seal.abalon.se>, ken1@abalon.se says...
- >
- >Is it possible to know the size of the vtbl for a given class
- >at compile time (to do something like
- >
- >int x = sizeof(theclass::vtbl)
- >
- >or similar)?
- >
-
- If you have control over the base class, you can
- evaluate the offset of the first data member with code like
-
- int sz_vtab = ((int)(&((myclass *)1)->data)-1);
-
- Assuming all vtables has the same size, you could also evaluate
- the size of a class that is nothing but a virtual function
-
- class MyHiddenLocalClass {
- virtual ~MyHiddenLocalClass();
- };
-
- int sz_vtab = sizeof(MyHiddenLocalClass);
-
- Both approaches might fail if the compiler aligns data members and
- rounds struct/class sizes in unpredicted ways. Strictly speaking,
- the vtable itself is not required by the standard, but its effects.
- So this is not strictly portable code, but code that you can prove
- works for all interesting platforms ;-)
-
- // Urban
-
-